<?php
class core_attendance extends object {

public $id="";
public $uid="";
public $day="";
public $att="";

function var_validate() {
	$out="";	
	$loc = new loc;
	if ($this->uid == "") {
		$out=$out."<br>".$loc->attendance_missing_uid; 		
		}		
	if ($this->day == "") {
		$out=$out."<br>".$loc->attendance_missing_day; 		
		}
	if ($this->att == "") {
		$out=$out."<br>".$loc->attendance_missing_att; 		
		}
	return $out;	
	}

function add_validate() {
	$out="";	
	$loc = new loc;
	$validation=$this->var_validate();
	if ($validation == "") {
		$query="select * from attendance where uid=".mysql_real_escape_string($this->uid)." AND day = ".mysql_real_escape_string($this->day)."";
		$result=mysql_query($query);
		if ($result != "") {
			while ($row=mysql_fetch_array($result)) {$out=$out."<br>".$loc->attendance_already_set;}
			} 
		} else {$out=$out."<br>".$validation;}
	return $out; 		 	
	}

function add() {
	$validation=$this->add_validate();
	if ($validation == "") {
		$query="insert into attendance (uid,day,att) values (".mysql_real_escape_string($this->uid).",".mysql_real_escape_string($this->day).",".mysql_real_escape_string($this->att).")";
		mysql_query($query);
			
		}
	}
	
function edit_validate() {
	$out="";	
	$loc = new loc;
	if ($this->id == "") {
		$out=$out."<br>".$loc->attendance_missing_id; 		
		}
	if ($this->att == "") {
		$out=$out."<br>".$loc->attendance_missing_att; 		
		}
	return $out;
	}
	
function edit() {
	$validation=$this->edit_validate();
	if ($validation=="") {
		$query="update attendance set att=".mysql_real_escape_string($this->att)." where id=".mysql_real_escape_string($this->id)."";
		mysql_query($query);	
		//return $query;		
		}	
	}
	
function computefromtasks() {
	$att="0";
	$this->day=$_SESSION['date2'];
	$this->uid=$_SESSION['uid'];
	$datetime=new core_time;
	$datetime->date=$this->day;
	$datetime->time='00:01';
	$timestart=strtotime($_SESSION['date']." 00:00:01");
	$datetime->time='23:59';
	$timestop=strtotime($_SESSION['date']. " 23:59:59");
	$query="select * from userjobs where from_ > ".$timestart." AND from_ < ".$timestop." AND uid=".$_SESSION['uid']." ORDER BY from_";
	$result=mysql_query("$query");
	if ($result != "") {
		while ($row=mysql_fetch_array($result)) {
			if($row['duration'] != ""){$att=$att+$row['duration'];}
			}
		}
		
		
	$date=new core_time;
	$date->date=$_SESSION['date2'];
	$date->time="00:00";
	//$timestamp=$date->totimestamp();
	$timestamp=strtotime($_SESSION['date']);
	$query="select id from attendance where day=".$timestamp." AND uid=".$_SESSION['uid']."";
	$result=mysql_query($query);
	$row=mysql_fetch_array($result);
	if ($row['id'] == "") {
	$this->uid=$_SESSION['uid'];
	$this->day=$timestamp;
	$this->att=$att;
	$this->add();
	} else {
	$this->uid=$_SESSION['uid'];
	$this->id=$row['id'];
	$this->att=$att;
	$test=$this->edit();
	}	
		
	
	//return $this->add_validate();	
	return $datetime->tohm($att);
	}

function computefromtasks_date($dat) {
	$att="0";
	$this->day=$dat;
	$this->uid=$_SESSION['uid'];
	$datetime=new core_time;
	$datetime->date=$this->day;
	$datetime->time='00:01';
	$timestart=strtotime($dat." 00:00:01");
	$datetime->time='23:59';
	$timestop=strtotime($dat." 23:59:59");
	$query="select * from userjobs where from_ >= ".$timestart." AND to_ <= ".$timestop." AND uid=".$_SESSION['uid']." ORDER BY from_";
	$result=mysql_query("$query");
	if ($result != "") {
		while ($row=mysql_fetch_array($result)) {
			if($row['duration'] != ""){$att=$att+$row['duration'];}
			}
		}
		
		
	$date=new core_time;
	$date->date=$dat;
	$date->time="00:00";
	$timestamp=strtotime($dat);
	$query="select id from attendance where day=".$timestamp." AND uid=".$_SESSION['uid']."";
	$result=mysql_query($query);
	$row=mysql_fetch_array($result);
	if ($row['id'] == "") {
	$this->uid=$_SESSION['uid'];
	$this->day=$timestamp;
	$this->att=$att;
	$this->add();
	} else {
	$this->uid=$_SESSION['uid'];
	$this->id=$row['id'];
	$this->att=$att;
	$test=$this->edit();
	}	
		
		
	return $datetime->tohm($att);
	//return $dat." ".$_SESSION['date'];
	}
	
}



?>
